/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.article;

import ejb.model.EJBModel;
import java.util.ArrayList;
import java.util.Collection;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import listeners.table.TableKeyListener;
import model.article.EancodeTableModel;
import model.article.ModelTableModel;
import model.article.PriceTableModel;
import model.article.SizeTableModel;
import model.gui.MenuModel;
import util.article.ArticleDetails;
import util.article.EancodeDetails;
import util.article.GroupDetails;
import util.article.ImageDetails;
import util.article.ModelDetails;
import util.article.PriceClassDetails;
import util.article.PriceDetails;
import util.article.SizeDetails;
import util.article.StockDetails;
import util.person.SupplierDetails;

/**
 *
 * @author Joris
 */
public class newArticle extends javax.swing.JPanel implements TableModelListener{
    
    /**
     * Creates new form newArticle
     */
    private MenuModel model;
    private DefaultComboBoxModel<GroupDetails> groupComboBoxModel;
    private ArrayList<GroupDetails> roots;
    private JComboBox<ModelDetails> modelCmb;
    private DefaultComboBoxModel<ModelDetails> modelComboBoxModel;
    private JComboBox<SizeDetails> sizeCmb;
    private DefaultComboBoxModel<SizeDetails> sizeComboBoxModel;
    
    public newArticle(MenuModel model) {
        this.model = model;
        groupComboBoxModel = new DefaultComboBoxModel<GroupDetails>();
        modelComboBoxModel = new DefaultComboBoxModel<ModelDetails>();
        modelCmb = new JComboBox<ModelDetails>(modelComboBoxModel);
        sizeComboBoxModel = new DefaultComboBoxModel<SizeDetails>();
        sizeCmb = new JComboBox<SizeDetails>(sizeComboBoxModel);
        initComponents();
        getAllRootGroups();
        modelModel.addTableModelListener(this);
        sizeModel.addTableModelListener(this);
        jTable1.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(modelCmb));
        jTable1.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(sizeCmb));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        supplierTxt = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        articleNumberTxt = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        descriptionTxt = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        sizeModel = new SizeTableModel();
        jTable3 = new javax.swing.JTable();
        jLabel4 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        modelModel = new ModelTableModel();
        jTable2 = new javax.swing.JTable();
        saveBtn = new javax.swing.JButton();
        addSizesBtn = new javax.swing.JButton();
        jTable3.addKeyListener(new TableKeyListener(sizeModel, jTable3));
        jScrollPane3 = new javax.swing.JScrollPane();
        eancodeModel = new EancodeTableModel(modelModel);
        jTable1 = new javax.swing.JTable();
        addAllEanBtn = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane4 = new javax.swing.JScrollPane();
        priceModel = new PriceTableModel();
        jTable4 = new javax.swing.JTable();

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Fabrikant");

        supplierTxt.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Artikelnummer");

        articleNumberTxt.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Omschrijving");

        descriptionTxt.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        jTable3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jTable3.setModel(sizeModel);
        jScrollPane1.setViewportView(jTable3);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("Groep");

        jComboBox1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jComboBox1.setModel(groupComboBoxModel);

        jTable2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jTable2.setModel(modelModel);
        jTable2.addKeyListener(new TableKeyListener(modelModel, jTable2));
        jScrollPane2.setViewportView(jTable2);

        saveBtn.setText("Opslaan");
        saveBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveBtnActionPerformed(evt);
            }
        });

        addSizesBtn.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        addSizesBtn.setText("Maten toevoegen");
        addSizesBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addSizesBtnActionPerformed(evt);
            }
        });

        jTable1.setModel(eancodeModel);
        jTable1.addKeyListener(new TableKeyListener(eancodeModel, jTable1));
        jScrollPane3.setViewportView(jTable1);

        addAllEanBtn.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        addAllEanBtn.setText("Eancodes voor alle maten en modellen");
        addAllEanBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addAllEanBtnActionPerformed(evt);
            }
        });

        jButton2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButton2.setText("Eancodes voor alle maten");

        jButton3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButton3.setText("Eancodes voor alle modellen");

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jTable4.setModel(priceModel);
        jScrollPane4.setViewportView(jTable4);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(saveBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(supplierTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(articleNumberTxt))
                            .addComponent(descriptionTxt)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(addSizesBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButton3)
                    .addComponent(addAllEanBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane3))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 803, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel1)
                                    .addComponent(supplierTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel2)
                                    .addComponent(articleNumberTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(34, 34, 34)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel3)
                                    .addComponent(descriptionTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel4)
                                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(33, 33, 33)
                                        .addComponent(addSizesBtn)))
                                .addGap(18, 18, 18)
                                .addComponent(addAllEanBtn)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton3)
                                .addGap(8, 8, 8)
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE))
                            .addComponent(jSeparator1))
                        .addGap(18, 18, 18))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(92, 92, 92)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addComponent(saveBtn)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBtnActionPerformed
        ArticleDetails ad = new ArticleDetails();
        ad.setArticleNumber(articleNumberTxt.getText());
        ad.setDescription(descriptionTxt.getText());
        SupplierDetails s = new SupplierDetails(1, TOOL_TIP_TEXT_KEY, WIDTH, null, WIDTH, Long.MIN_VALUE);
        ad.setSupplier(s);
        GroupDetails g = new GroupDetails();
        g.setId(1);
        ad.setGroup(g);
        PriceClassDetails pcd = new PriceClassDetails(1, TOOL_TIP_TEXT_KEY, TOOL_TIP_TEXT_KEY);
        PriceDetails pd = new PriceDetails();
        pd.setCoefficient(2.45);
        pd.setPrice(16.5);
        pd.setPurchase(7.5);
        pd.setDiscount(30);
        pd.setPriceClass(pcd);
        ArrayList<PriceDetails> prices = new ArrayList<PriceDetails>();
        prices.add(pd);
        ad.setPriceCollection(prices);
        ad.setEancodeCollection(new ArrayList<EancodeDetails>());
        ad.setImageCollection(new ArrayList<ImageDetails>());
        ArrayList<ModelDetails> models = new ArrayList<ModelDetails>();
        models.add(new ModelDetails(-1, "Blauw", "A"));
        models.add(new ModelDetails(-1, "Rood", "B"));
        ad.setModelCollection(models);
        ad.setSizeCollection(new ArrayList<SizeDetails>());
        ad.setStockCollection(new ArrayList<StockDetails>());
        
        Long t1 = System.currentTimeMillis();
        EJBModel.getArticleBean().createArticle(ad);
        Long t2 = System.currentTimeMillis();
        model.setStatus("Article saved in " + (t2-t1) + " ms.");
    }//GEN-LAST:event_saveBtnActionPerformed

    
    private void getAllRootGroups(){
        Long t1 = System.currentTimeMillis();
        roots = new ArrayList<GroupDetails>(EJBModel.getArticleGroupBean().getAllRootGroups());
        populateGroupComboBox(groupComboBoxModel);
        Long t2 = System.currentTimeMillis();
        model.setStatus("Collected all groups in " + (t2-t1) + " ms");
    }
    
    private void populateGroupComboBox(DefaultComboBoxModel cmbModel){
        cmbModel.removeAllElements();
        for(GroupDetails root:roots){
            cmbModel.addElement(root);
            if(root.getChildrenGroups() != null){
                for(GroupDetails child:root.getChildrenGroups())
                    addComboboxItem(cmbModel, child);
            }
        }   
    }
    
    private void addComboboxItem(DefaultComboBoxModel cmbModel, GroupDetails gd){
        cmbModel.addElement(gd);
        if(gd.getChildrenGroups() != null){
                for(GroupDetails childGroup:gd.getChildrenGroups())
                    addComboboxItem(cmbModel, childGroup);
            }
    }
    
    private void addSizesBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addSizesBtnActionPerformed
        SizesDialog dialog = new SizesDialog(null, true);
        dialog.pack();
        dialog.setVisible(true);
        if(dialog.addSizes())
            sizeModel.addSizes(dialog.getSelectedSizes());
            
    }//GEN-LAST:event_addSizesBtnActionPerformed

    private void addAllEanBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addAllEanBtnActionPerformed
        Collection<ModelDetails> models = modelModel.getAllModels();
        Collection<SizeDetails> sizes = sizeModel.getAllSizes();
        ArrayList<EancodeDetails> eancodes = new ArrayList<EancodeDetails>();
        for(ModelDetails modelDetail:models){
            for(SizeDetails size:sizes){
                EancodeDetails eancode = new EancodeDetails(-1, -1, size, modelDetail);
                eancodes.add(eancode);
            }
        }
        eancodeModel.addEancodes(eancodes);
        
    }//GEN-LAST:event_addAllEanBtnActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addAllEanBtn;
    private javax.swing.JButton addSizesBtn;
    private javax.swing.JTextField articleNumberTxt;
    private javax.swing.JTextField descriptionTxt;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSeparator jSeparator1;
    private EancodeTableModel eancodeModel;
    private javax.swing.JTable jTable1;
    private ModelTableModel modelModel;
    private javax.swing.JTable jTable2;
    private SizeTableModel sizeModel;
    private javax.swing.JTable jTable3;
    PriceTableModel priceModel;
    private javax.swing.JTable jTable4;
    private javax.swing.JButton saveBtn;
    private javax.swing.JTextField supplierTxt;
    // End of variables declaration//GEN-END:variables

    @Override
    public void tableChanged(TableModelEvent e) {
        if(e.getSource() == modelModel){
            modelComboBoxModel.removeAllElements();
            for(ModelDetails modelDetail:modelModel.getAllModels())
                modelComboBoxModel.addElement(modelDetail);
        }
        if(e.getSource() == sizeModel){
            sizeComboBoxModel.removeAllElements();
            for(SizeDetails sizeDetail:sizeModel.getAllSizes())
                sizeComboBoxModel.addElement(sizeDetail);
        }
    }
}
